#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import re
import time
import codecs
import pymongo
import sys
import random
import datetime
sys.path.insert(0,'/home/leeray/workspaces/Python/wanalytics/')
from Tools import getPid
from Tools import connectMySQL 

pid_json = []

def getPid():
    global pid_json
    db = connectMySQL.connMySQL_mobileProduction()
    c=db.cursor()
    sql = "select pid, os_id, s_partner_pid_type, f_id from s_partner"
    c=db.cursor()
    c.execute(sql)
    res= c.fetchall()
    for x in res:
        pid_json.append({'pid':str(x[0]), 'osid': int(x[1]), 'pidtype': int(x[2]), 'fid' : int(x[3])})
    return pid_json

def clientmain(date):
    db = connectMySQL.connMySQL_stat()
    c=db.cursor()
    for i in range(len(pid_json)):
        pidjson = pid_json[i]
        if not pidjson :
            continue
        pid = pidjson.get('pid' , '')
        pid_type = pidjson.get('pidtype', 0)
        osid = pidjson.get('osid', 0)
        pv = random.randint(10000, 99999)
        uv = random.randint(1000, 9999)
        vv = random.randint(30000, 79999)
        register = random.randint(100, 999)
        upload = random.randint(1000, 9999)
        newuser = random.randint(100, 999)
        alluser = random.randint(1000, 9999)
        if str(pid_type) in '1, 2, 3, 4, 5': 
            c.execute("""INSERT INTO `stat_day_main`(pid, os_id, pid_type,  date, pv, uv, vv, register, upload, newuser,alluser) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s , %s, %s) """, (pid, osid, pid_type, date, pv, uv, vv, register, upload, newuser, alluser ))

def clientfactory(date):
    db = connectMySQL.connMySQL_stat()
    c=db.cursor()
    for i in range(len(pid_json)):
        pidjson = pid_json[i]
        if not pidjson :
            continue
        pid = pidjson.get('pid' , '')
        pid_type = pidjson.get('pidtype', 0)
        osid = pidjson.get('osid', 0)
        fid = pidjson.get('fid', 0)
        pv = random.randint(10000, 99999)
        uv = random.randint(1000, 9999)
        vv = random.randint(30000, 79999)
        register = random.randint(100, 999)
        upload = random.randint(1000, 9999)
        newuser = random.randint(100, 999)
        alluser = random.randint(1000, 9999)
        if pid_type  == 10: 
            c.execute("""INSERT INTO `stat_day_factory`(pid, os_id, f_id,  date, pv, uv, vv, register, upload, newuser, alluser) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s , %s, %s) """, (pid, osid, fid, date, pv, uv, vv, register, upload, newuser, alluser ))

def clientchannel(date):
    db = connectMySQL.connMySQL_stat()
    c=db.cursor()
    for i in range(len(pid_json)):
        pidjson = pid_json[i]
        if not pidjson :
            continue
        pid = pidjson.get('pid' , '')
        pid_type = pidjson.get('pidtype', 0)
        osid = pidjson.get('osid', 0)
        fid = pidjson.get('fid', 0)
        pv = random.randint(10000, 99999)
        uv = random.randint(1000, 9999)
        vv = random.randint(30000, 79999)
        register = random.randint(100, 999)
        upload = random.randint(1000, 9999)
        newuser = random.randint(100, 999)
        alluser = random.randint(1000, 9999)
        if pid_type  == 9: 
            c.execute("""INSERT INTO `stat_day_channel`(pid, f_id,  date, pv, uv, vv, register, upload, newuser,alluser) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s , %s) """, (pid, fid, date, pv, uv, vv, register, upload, newuser, alluser ))

def clientpaike(date):
    db = connectMySQL.connMySQL_stat()
    c=db.cursor()
    for i in range(len(pid_json)):
        pidjson = pid_json[i]
        if not pidjson :
            continue
        pid = pidjson.get('pid' , '')
        pid_type = pidjson.get('pidtype', 0)
        
        if str(pid_type) not in '6, 7': 
            continue
        
        osid = pidjson.get('osid', 0)
        pv = random.randint(10000, 99999)
        uv = random.randint(1000, 9999)
        vv = random.randint(30000, 79999)
        register = random.randint(100, 999)
        upload = random.randint(1000, 9999)
        newuser = random.randint(100, 999)
        alluser = random.randint(1000, 9999)        
        c.execute("""INSERT INTO `stat_day_paike`(pid, os_id, pid_type,  date, pv, uv, vv, register, upload, newuser,alluser) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s , %s, %s) """, (pid, osid, pid_type, date, pv, uv, vv, register, upload, newuser, alluser ))


def main():
    global pid_json
    pid_json = getPid()
    date0 = sys.argv[1]
    date1 = sys.argv[2]
    stime = datetime.datetime.strptime(date0, '%Y%m%d')
    etime = datetime.datetime.strptime(date1, '%Y%m%d')
    days = (etime - stime).days
    tmptime = stime
    for i in range(days):
        timestr =  tmptime.strftime('%Y%m%d')
        #clientmain(timestr)
        #clientfactory(timestr)
        #clientchannel(timestr)
        clientpaike(timestr)
        tmptime = tmptime + datetime.timedelta(days=1)

if __name__ == '__main__':
    main()
    

    